<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="dataForm.templateName"
          placeholder="模板名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="模板说明" prop="description">
        <el-input
          v-model="dataForm.description"
          placeholder="模板说明"
        ></el-input>
      </el-form-item>
      <el-form-item label="发件人" prop="senderId">
        <el-select v-model="dataForm.senderId" @change="jumpTypeChange">
          <el-option
            v-for="item in members"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="消息标题"></el-input>
      </el-form-item>
      <el-form-item label="消息内容" prop="content">
        <el-input
          type="textarea"
          v-model="dataForm.content"
          placeholder="消息内容，动态内容可使用 #{} 来占位"
          style="height: 100%"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="dataForm.status"
          active-value="1"
          inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        ></el-switch>
      </el-form-item>
      <el-form-item label="跳转方式" prop="jumpType">
        <el-select v-model="dataForm.jumpType" @change="jumpTypeChange">
          <el-option
            v-for="item in jumpTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跳转路径" prop="jumpUrl">
        <el-input
          v-model="dataForm.jumpUrl"
          placeholder="跳转方式为不跳转时，此处可不写"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        templateName: "",
        description: "",
        senderId: "",
        title: "",
        content: "",
        jumpType: "",
        jumpUrl: "",
        status: "1"
      },
      jumpTypes: [
        {
          value: "0",
          label: "不跳转",
        },
        {
          value: "1",
          label: "站内跳转",
        },
        {
          value: "2",
          label: "站外跳转",
        },
      ],
      members: [],
      dataRule: {
        templateName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "模板说明不能为空", trigger: "blur" },
        ],
        senderId: [
          { required: true, message: "发件人不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        jumpType: [
          { required: true, message: "跳转类型不能为空", trigger: "blur" },
        ],
        jumpUrl: [
          { required: false, message: "跳转链接不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(members,id) {
      this.members=members;
      this.dataForm.id = id || 0;
      this.visible = true;
      this.queryMembers();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.queryTemplate(this.dataForm.id)
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/forum/messagetemplate/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              templateName: this.dataForm.templateName,
              description: this.dataForm.description,
              senderId: this.dataForm.senderId,
              title: this.dataForm.title,
              content: this.dataForm.content,
              jumpType: this.dataForm.jumpType,
              jumpUrl: this.dataForm.jumpUrl,
              status: this.dataForm.status,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              deleted: this.dataForm.deleted,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    queryMembers() {
      this.$http({
        url: this.$http.adornUrl("/forum/member/multiConditList"),
        method: "post",
        data: this.$http.adornData(this.memberQueryObj),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.members=data.page.list;
        }
      });
    },
    queryTemplate(id) {
      this.$http({
        url: this.$http.adornUrl(
          `/forum/messagetemplate/info/${id}`
        ),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataForm.templateName = data.messageTemplate.templateName;
          this.dataForm.description = data.messageTemplate.description;
          this.dataForm.senderId = data.messageTemplate.senderId;
          this.dataForm.title = data.messageTemplate.title;
          this.dataForm.content = data.messageTemplate.content;
          this.dataForm.jumpType = data.messageTemplate.jumpType;
          this.dataForm.jumpUrl = data.messageTemplate.jumpUrl;
          this.dataForm.status = data.messageTemplate.status;
        }
      });
    },
    jumpTypeChange(value){
        if(value==0){
          this.dataRule.jumpUrl[0].required=false;
        }else{
          this.dataRule.jumpUrl[0].required=true;
        }
    }
  },
};
</script>
